package com.quasar.hpatient.view.adapter.render;

import android.content.Context;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ada.adapter.RenderAdapter;
import com.ada.adapter.RenderBuiler;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.custommodel.OutJionValueDataBean;
import com.quasar.hpatient.model.litePalmodel.DoseInfodb;
import com.quasar.hpatient.model.litePalmodel.DoseModedb;
import com.quasar.hpatient.model.litePalmodel.DosePerioddb;
import com.quasar.hpatient.model.litePalmodel.MedicineInfodb;
import com.quasar.hpatient.model.medicalmodel.PrescriptionDayData;
import com.quasar.hpatient.utils.DateUtil;
import com.quasar.hpatient.utils.Spanny;
import com.quasar.hpatient.view.prescription.AddPrescriptionActivity_;
import com.quasar.hpatient.widget.MyListView;
import com.vise.log.ViseLog;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class MedicationListRender extends RenderBuiler<PrescriptionDayData.ListBean.PerscriptDetailListBean> implements View.OnClickListener {

    TextView tv_drugs_name,prescription_items_tv_content,prescription_items_tv_spacce;//药品名称和规格以及服药内容
    MyListView mly_list;
    LinearLayout ll_layout;
    private String doseName;//规格
    private String stats;

    List<DoseModedb> doseModedbs=MainAppliaction.getInstance().getDoseModeList();

    public String getStats() {
        return stats;
    }

    public void setStats(String stats) {
        this.stats = stats;
    }

    public String getDoseName() {
        return doseName;
    }

    public void setDoseName(String doseName) {
        this.doseName = doseName;
    }

    public MedicationListRender(Context context) {
        super(context);
    }

    List<DosePerioddb> dosePerioddbList = MainAppliaction.getInstance().getDosePeriodList();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_layout:
                //跳转到修改药单界面
                if (getStats() != null && !getStats().equals("PharmacyRecordActivity")) {
                    AddPrescriptionActivity_.intent(getContext()).DataTime(getContent().getPrescriptionIndex().getEditTime())
                            .ListPrescription(getPrescriptionListBean())
                            .prescriptionDay(getContent()).start();
                }
                break;
        }
    }

    RenderAdapter<PrescriptionDayData.ListBean.PerscriptDetailListBean.PrescriptionDoseDetailBean> adapter;

    private int color(int value) {
        return getContext().getResources().getColor(value);
    }

    @Override
    public void render() {
        String fuyao = "";//服药内容
        String days = "";

        List<PrescriptionDayData.ListBean.PerscriptDetailListBean.PrescriptionDoseDetailBean> prescriptionDoseDetailBeanList=new ArrayList<>();

        for(int i=0;i<getContent().getPrescriptionDoseDetail().size();i++){
            prescriptionDoseDetailBeanList.add(getContent().getPrescriptionDoseDetail().get(i));
        }



        Collections.sort(prescriptionDoseDetailBeanList, new Comparator<PrescriptionDayData.ListBean.PerscriptDetailListBean.PrescriptionDoseDetailBean>() {
            @Override
            public int compare(PrescriptionDayData.ListBean.PerscriptDetailListBean.PrescriptionDoseDetailBean o1, PrescriptionDayData.ListBean.PerscriptDetailListBean.PrescriptionDoseDetailBean o2) {
                Date date1 = DateUtil.stringToDate(o1.getUseTime() );
                Date date2 = DateUtil.stringToDate(o2.getUseTime());
                if ((date1).after(date2)) {
                    return 1;
                }
                return -1;
            }
        });

        for(int i=0;i<prescriptionDoseDetailBeanList.size();i++){
            prescriptionDoseDetailBeanList.get(i).setDetailId(i+1);
        }


        DrugsListRender drugsListRender = new DrugsListRender(getContext());
        drugsListRender.setSpecification(getContent().getDoseName());
        //规格
        drugsListRender.setDoseName(getContent().getDoseName());
        RenderBuiler<PrescriptionDayData.ListBean.PerscriptDetailListBean.PrescriptionDoseDetailBean> builder = drugsListRender;

        adapter = new RenderAdapter<>(getContext(), builder);


        if (prescriptionDoseDetailBeanList.size() != 0) {
            adapter.addAll(prescriptionDoseDetailBeanList);
            mly_list.setAdapter(adapter);
        }

        if (getStats() != null) {
            if (getStats().equals("PharmacyRecordActivity")) {
                mly_list.setVisibility(View.GONE);
            }
        }


        //服药次数
        if (getContent().getPrescriptionDoseDetail() != null) {//服药次数
            fuyao = getContent().getPrescriptionDoseDetail().size() + "";
        }

        for (int i = 0; i < dosePerioddbList.size(); i++) {
            if (dosePerioddbList.get(i).getPrimaryid() == getContent().getUserPeriodId()) {
                days = dosePerioddbList.get(i).getName();
                break;
            }
        }

        if (getContent().getPrescriptionDoseDetail() != null) {
            if (getContent().getPrescriptionDoseDetail().size() > 0) {
                 StringBuffer stringBuffer=new StringBuffer();
                for(int i=0;i<doseModedbs.size();i++){
                    if(getContent().getUseTypeId().equals(doseModedbs.get(i).getPrimaryid()+"")){
                        stringBuffer.append(doseModedbs.get(i).getName());
                        break;
                    }
                }
                prescription_items_tv_content.setText(stringBuffer.toString()+ getContent().getPrescriptionDoseDetail().size() + "次");
//                prescription_items_tv_spacce.setText("\n");//取消底部的空白
            }
        }

        Spanny spanny = new Spanny();

        if(getStats()!=null){
            if(getStats().equals("AtPrescriptionActivity") || getStats().equals("HistoryPrescriptionActivity")){
                spanny.append(getContent().getMName(), new ForegroundColorSpan(color(R.color.btcolor)));
                if(getContent().getDoseName()!=null && getContent().getDoseName().length()>0){
                    spanny.append("\t(\t"+ getContent().getDoseName() + "\t)\t", new ForegroundColorSpan(color(R.color.btcolor)));
                }
            }else{
                spanny.append(getContent().getMName(), new ForegroundColorSpan(color(R.color.textheadcolor)));
                if(getContent().getDoseName()!=null && getContent().getDoseName().length()>0){
                    spanny.append("\t(\t"+ getContent().getDoseName() + "\t)\t", new ForegroundColorSpan(color(R.color.textheadcolor)));
                }
            }
        }

        tv_drugs_name.setText(spanny);
    }

    @Override
    protected void setUpView(View view) {
        tv_drugs_name = (TextView) view.findViewById(R.id.tv_drugs_name);
        mly_list = (MyListView) view.findViewById(R.id.mly_list);
        ll_layout = (LinearLayout) view.findViewById(R.id.ll_layout);
        prescription_items_tv_content= (TextView) view.findViewById(R.id.prescription_items_tv_content);
        prescription_items_tv_spacce= (TextView) view.findViewById(R.id.prescription_items_tv_spacce);
    }

    @Override
    protected void hookListeners(View view) {
        ll_layout.setOnClickListener(this);
        mly_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到修改药单界面
                if (getStats() != null && !getStats().equals("PharmacyRecordActivity")) {
                    AddPrescriptionActivity_.intent(getContext()).DataTime(getContent().getPrescriptionIndex().getEditTime())
                            .ListPrescription(getPrescriptionListBean())
                            .prescriptionDay(getContent()).start();
                }
            }
        });

    }

    @Override
    protected int getlayoutid() {
        return R.layout.medication_list_layout;
    }




    //获取所有药单数据
    private PrescriptionDayData.ListBean PrescriptionListBean;

    public PrescriptionDayData.ListBean getPrescriptionListBean() {
        return PrescriptionListBean;
    }

    public void setPrescriptionListBean(PrescriptionDayData.ListBean prescriptionListBean) {
        PrescriptionListBean = prescriptionListBean;
    }
}
